﻿using System;

namespace Tomato.Allure
{
	internal class DocumentImpl : Tomato.Allure.IDocument
	{
		private EnvDTE.Document m_instance;

		public Tomato.Allure.IProjectItem ProjectItem
		{
			get { return new ProjectItemImpl( m_instance.ProjectItem ); }
		}

		public string Name
		{
			get { return m_instance.Name; }
		}

		public string Path
		{
			get { return m_instance.Path; }
		}

		public string FullName
		{
			get { return m_instance.FullName; }
		}

		public string Kind
		{
			get { return m_instance.Kind; }
		}

		public bool IsReadOnly
		{
			get { return m_instance.ReadOnly; }
		}

		public bool IsSaved
		{
			get { return m_instance.Saved; }
		}

		public string ExtenderCATID
		{
			get { return m_instance.ExtenderCATID; }
		}

		public int IndentSize
		{
			get { return m_instance.IndentSize; }
		}

		public string Language
		{
			get { return m_instance.Language; }
		}

		public int TabSize
		{
			get { return m_instance.TabSize; }
		}

		public Tomato.Allure.IWindow ActiveWindow
		{
			get { return new WindowImpl( m_instance.ActiveWindow ); }
		}

		public Tomato.Allure.IWindows Windows
		{
			get { return new WindowsImpl( m_instance.Windows ); }
		}

		public DocumentImpl( EnvDTE.Document instance )
		{
			if( instance == null ) { throw new NullReferenceException(); }

			m_instance = instance;
		}		

		public void Activate()
		{
			m_instance.Activate();
		}

		public void ClearBookmarks()
		{
			m_instance.ClearBookmarks();
		}

		public void Close( Tomato.Allure.SaveChanges saveChanges )
		{
			EnvDTE.vsSaveChanges closeOption = EnvDTE.vsSaveChanges.vsSaveChangesNo;
			switch( saveChanges )
			{
				//case Allure.SaveChanges.No: closeOption = vsSaveChanges.vsSaveChangesNo; break;
				case Tomato.Allure.SaveChanges.Prompt: closeOption = EnvDTE.vsSaveChanges.vsSaveChangesPrompt; break;
				case Tomato.Allure.SaveChanges.Yes: closeOption = EnvDTE.vsSaveChanges.vsSaveChangesYes; break;
			}

			m_instance.Close( closeOption ); 
		}

		public Tomato.Allure.IWindow NewWindow()
		{
			return new WindowImpl( m_instance.NewWindow() );
		}

		public void PrintOut()
		{
			m_instance.PrintOut();
		}

		public bool Redo()
		{
			return m_instance.Redo();
		}

		public bool Undo()
		{
			return m_instance.Undo();
		}

		public bool ReplaceText( string findText, string replaceText, int flags )
		{
			return m_instance.ReplaceText( findText, replaceText, flags );
		}

		public Tomato.Allure.SaveStatus Save( string filePath )
		{
			EnvDTE.vsSaveStatus saveStatus = m_instance.Save( filePath );

			switch( saveStatus )
			{
				case EnvDTE.vsSaveStatus.vsSaveCancelled: return Tomato.Allure.SaveStatus.Cancelled;
				case EnvDTE.vsSaveStatus.vsSaveSucceeded: return Tomato.Allure.SaveStatus.Succeeded;
			}

			return Tomato.Allure.SaveStatus.Cancelled;
		}
	}
}
